using UnityEngine.Networking;
namespace Framework
{
    //读取文件到Buffer中
    public class DownloadFileBufferHandler : DownloadHandlerScript
    {
        public int Length { get; private set; }
        public byte[] Buffer { get; private set; }
        private int _contentLength;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer">交换数据的缓存</param>
        /// <returns></returns>
        public DownloadFileBufferHandler(byte[] buffer) : base(buffer)
        {

        }
#if UNITY_2021_1_OR_NEWER
        public override void Dispose()
        {
            base.Dispose();
            Buffer = null;
        }
#endif
        //设置要存储的目标
        public void SetBuffer(byte[] value)
        {
            Buffer = value;
            Length = 0;
        }
        protected override bool ReceiveData(byte[] data, int dataLength)
        {
            if (data == null || data.Length < 1)
            {
                return false;
            }
            System.Buffer.BlockCopy(data, 0, Buffer, Length, dataLength);
            Length += dataLength;
            return true;
        }
        protected override byte[] GetData()
        {
            return null;
        }
        protected override void ReceiveContentLengthHeader(ulong contentLength)
        {
            _contentLength = (int)contentLength;
            // FrameworkLog.LogError("contentLength: " + contentLength);
        }
        protected override void CompleteContent()
        {
            // FrameworkLog.LogError("length: " + Length);
        }
        protected override float GetProgress()
        {
            if (_contentLength < 1) return 0f;
            return (float)Length / (float)_contentLength;
        }
    }
}